import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/admin/Home.vue'
import Welcome from "../components/admin/Welcome"
import UserList from "../components/admin/UserList"
import AdminList from "../components/admin/AdminList";
import FactoryList from "../components/admin/FactoryList";
import EquipType from "../components/admin/EquipType";
import ProductType from "../components/admin/ProductType";
import ProductList from "../components/admin/ProductList";
import EquipList from "../components/admin/EquipList";
import MyFactory from "../components/factory/MyFactory";
import WelcomeFactory from "../components/factory/WelcomeFactory";
import MyOrder from "../components/factory/MyOrder";
import MyEquip from "../components/factory/MyEquip";
import RentList from "../components/factory/RentList";
import ConfigList from "../components/factory/ConfigList";
import AgentHome from "../components/agent/AgentHome";
import WelcomeAgent from "../components/agent/WelcomeAgent";
import OrderList from "../components/agent/OrderList";
import BideList from "../components/agent/BideList";
import ReleasedOrderList from "../components/factory/ReleasedOrderList";
import PlanList from "../components/factory/PlanList";
import Register from "../components/Register";
import AllOrderList from "../components/admin/AllOrderList";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path:'/register',
    component: Register
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children:[
      {path:'/welcome',component: Welcome},
      {path:'/user',component: UserList},
      {path:'/adminList',component: AdminList},
      {path:'/factoryList',component: FactoryList},
      {path:'/equipType',component: EquipType},
      {path:'/productType',component: ProductType},
      {path:'/productList',component: ProductList},
      {path:'/equipList',component: EquipList},
      {path:'/allOrderList',component: AllOrderList}
    ]
  },
  {
    path: '/myFactory',
    component: MyFactory,
    redirect: '/welcomeFactory',
    children: [
      {path:'/welcomeFactory',component: WelcomeFactory},
      {path:'/myOrder',component: MyOrder},
      {path:'/myEquip',component: MyEquip},
      {path:'/rentList',component: RentList},
      {path:'/configList',component: ConfigList},
      {path: '/releasedOrderList',component: ReleasedOrderList},
      {path:'/planList',component: PlanList}
    ]
  },
  {
    path: '/agentHome',
    component: AgentHome,
    redirect: '/welcomeAgent',
    children: [
      {path: '/welcomeAgent',component: WelcomeAgent},
      {path: '/orderList',component: OrderList},
      {path: '/bideList',component: BideList},
    ]
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to:将要访问的路径
  // from:从哪里访问的路径
  // next:之后要做的任务，是一个函数
  //    next（）放行， next（'/URL'）强制跳转的路径。
  if (to.path === '/login') return next();// 访问路径为登录
  if (to.path === '/register') return next();//访问路径为注册
  // 获取flag
  const flagStr = window.sessionStorage.getItem("flag");// session取值
  if (!flagStr) return next('/login');// 没登录去登录
  next();
})

export default router// 暴露出去
